package com.sskj.bibi.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.mvc.MVCHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.header.MaterialHeader;
import com.sskj.bibi.R;
import com.sskj.bibi.R2;
import com.sskj.bibi.bean.CoinBean;
import com.sskj.bibi.bean.EntrustBean;
import com.sskj.bibi.presenter.HistoryEntrustActivityPresenter;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

@Route(path = RConfig.BIBI_HISTORY_ENTRUST)
public class HistoryEntrustActivity extends BaseActivity<HistoryEntrustActivityPresenter> {
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.tvHistory)
    TextView tvHistory;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @BindView(R2.id.coinTypeSpinner)
    AppCompatSpinner coinTypeSpinner;
    @BindView(R2.id.tradeTypeSpinner)
    AppCompatSpinner tradeTypeSpinner;
    @BindView(R2.id.statusSpinner)
    AppCompatSpinner statusSpinner;
    @BindView(R2.id.btConfirm)
    Button btConfirm;
    @BindView(R2.id.filter_layout_root)
    LinearLayout filterLayoutRoot;
    @BindView(R2.id.view)
    View view;
    private SlimAdapter<EntrustBean> slimAdapter;
    String code;
    String type = null;
    String status = "4";
    private List<CoinBean> coinBeans;
    private HashMap<Integer, String> statusMap;
    private HashMap<Integer, String> typeMap;
    private MVCHelper mvcHelper;
    private HashMap<String, String> stateMap;

    @Override
    protected int getLayoutResId() {
        return R.layout.bibi_activity_history_entrust;
    }

    @Override
    public HistoryEntrustActivityPresenter getPresenter() {
        return new HistoryEntrustActivityPresenter();
    }

    @Override
    protected void initView() {
        stateMap = new HashMap<>();
        stateMap.put("0", App.INSTANCE.getString(R.string.bibiHistoryEntrustActivity3));
        stateMap.put("1", App.INSTANCE.getString(R.string.bibiHistoryEntrustActivity5));
        stateMap.put("2", App.INSTANCE.getString(R.string.bibiHistoryEntrustActivity7));
        stateMap.put("-1", App.INSTANCE.getString(R.string.bibiHistoryEntrustActivity9));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(false)
                .setLeftPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 29))
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 2))
                .setPaintColor(ContextCompat.getColor(App.INSTANCE, R.color.bibiMainLine))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.bibi_recy_item_entrust_history, new SlimInjector<EntrustBean>() {
            @Override
            public void onInject(EntrustBean data, IViewInjector injector, List<Object> payloads) {
                injector.text(R.id.entrust_type, data.getType().equals("1") ? App.INSTANCE.getString(R.string.bibiHistoryEntrustActivity11) : App.INSTANCE.getString(R.string.bibiHistoryEntrustActivity12))
                        .textColor(R.id.entrust_type, ContextCompat.getColor(App.INSTANCE, data.getType().equals("1") ? R.color.bibiGreenText : R.color.bibiRedText))
                        .text(R.id.entrust_name, data.getL_code() + "/" + data.getR_code())
                        .text(R.id.entrust_time, data.getTrade_time())
                        .text(R.id.entrust_prePrice, data.getOtype().equals("2") ? App.INSTANCE.getString(R.string.bibiHistoryEntrustActivity17) : NumberUtil.keep4(data.getWtprice()))
                        .text(R.id.entrust_prePrice_text, String.format(App.INSTANCE.getString(R.string.bibiHistoryEntrustActivity18), data.getR_code()))
                        .text(R.id.entrust_totalPrice, NumberUtil.keep4(data.getTotalprice()))
                        .text(R.id.entrust_totalPrice_text, String.format(App.INSTANCE.getString(R.string.bibiHistoryEntrustActivity19), data.getR_code()))
                        .text(R.id.entrust_realNum, NumberUtil.keep4(data.getCjnum()))
                        .text(R.id.entrust_realNum_text, String.format(App.INSTANCE.getString(R.string.bibiHistoryEntrustActivity20), data.getL_code()))
                        .text(R.id.entrust_realPrice, data.getCjprice())
                        .text(R.id.entrust_realPrice_text, String.format("成交均价%s", data.getR_code()))
                        .text(R.id.entrust_state, stateMap.get(data.getStatus() + ""));
                if (data.getOtype().equals("2") && data.getType().equals("1")) {  //市价买入
                    injector.text(R.id.entrust_preNum_text, String.format(App.INSTANCE.getString(R.string.bibiHistoryEntrustActivity24), data.getR_code()))
                            .text(R.id.entrust_preNum, NumberUtil.keep4(data.getTotalprice()));
                } else { //其他
                    injector.text(R.id.entrust_preNum_text, String.format(App.INSTANCE.getString(R.string.bibiHistoryEntrustActivity25), data.getL_code()))
                            .text(R.id.entrust_preNum, NumberUtil.keep4(data.getWtnum()));
                }
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ClickUtil.click(tvHistory, () -> {
            if (filterLayoutRoot.getVisibility() == View.VISIBLE) {
                filterLayoutRoot.setVisibility(View.GONE);
            } else {
                filterLayoutRoot.setVisibility(View.VISIBLE);
                if (coinTypeSpinner != null) {
                    coinTypeSpinner.setSelection(0);
                    statusSpinner.setSelection(0);
                    tradeTypeSpinner.setSelection(0);
                }
            }
        });
        ClickUtil.click(view, () -> {
            filterLayoutRoot.setVisibility(View.GONE);
        });
        coolRefreshView.setPullHeader(new MaterialHeader(this));
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        ModelRx2DataSource refreshDataSource = new ModelRx2DataSource<EntrustBean>(
                (ModelRx2DataSource.OnLoadSource<EntrustBean>)
                        page -> mPresenter.getEntrustHistoryFlow(code, type, status, page), 10);
        // 设置数据源
        mvcHelper.setDataSource(refreshDataSource);
        // 设置适配器
        mvcHelper.setAdapter(slimAdapter);
    }

    @Override
    protected void initData() {
        statusMap = new HashMap();
        statusMap.put(0, "4");
        statusMap.put(1, "2");
        statusMap.put(2, "-1");
        typeMap = new HashMap();
        typeMap.put(0, null);
        typeMap.put(1, "1");
        typeMap.put(2, "2");
        mPresenter.getAllCoin();
        mvcHelper.refresh();
    }

    public void updateUI(List<EntrustBean> data) {
        if (coolRefreshView.isRefreshing()) {
            coolRefreshView.setRefreshing(false);
        }
        slimAdapter.updateData(data);
    }

    public void updateCoinType(List<CoinBean> data) {
        this.coinBeans = data;
        ArrayList<String> items = new ArrayList<>();
        items.add(App.INSTANCE.getString(R.string.bibiHistoryEntrustActivity31));
        for (CoinBean c : data) {
            items.add(c.getMark());
        }
        ArrayAdapter<String> currencyAdapter = new ArrayAdapter(this, R.layout.bibi_dropdown_item, items);
        ArrayAdapter statusAdapter = new ArrayAdapter(this, R.layout.bibi_dropdown_item, getResources().getStringArray(R.array.bibiOrderStatus));
        ArrayAdapter typeAdapter = new ArrayAdapter(this, R.layout.bibi_dropdown_item, getResources().getStringArray(R.array.bibiOrderType));
        coinTypeSpinner.setAdapter(currencyAdapter);
        statusSpinner.setAdapter(statusAdapter);
        tradeTypeSpinner.setAdapter(typeAdapter);
        ClickUtil.click(btConfirm, () -> {
            filterLayoutRoot.setVisibility(View.GONE);
            int coinTypePos = coinTypeSpinner.getSelectedItemPosition();
            int statusTypePos = statusSpinner.getSelectedItemPosition();
            int tradeTypePos = tradeTypeSpinner.getSelectedItemPosition();
            if (coinTypePos == 0) {
                code = null;
            } else {
                code = coinBeans.get(coinTypePos - 1).getMark();
            }
            type = typeMap.get(tradeTypePos);
            status = statusMap.get(statusTypePos);
            coolRefreshView.setRefreshing(true);
        });
    }
}
